//
//  WFcategoryTableViewController.m
//  WangyiFangzuo
//
//  Created by 蔡靖方 on 16/9/14.
//  Copyright © 2016年 111. All rights reserved.
//

#import "WFcategoryTableViewController.h"
#import <AFNetworking.h>
#import "WFmodelHot.h"
#import "WFclassmodel.h"
#import "WFhotTableViewCell.h"
#import "WFlookbackTableViewCell.h"
@interface WFcategoryTableViewController ()
@property(nonatomic,strong)NSMutableArray *arryM;
@property(nonatomic,strong)NSMutableArray *arryMback;
@property(nonatomic,strong)UITableView *mytableview;
//头部标签
@property(nonatomic,strong)NSArray *arr;
@property(nonatomic,strong)UIView *titleviews;
@property(nonatomic,strong)UIButton *buttonRemb;
@end

@implementation WFcategoryTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
self.tableView.contentInset=UIEdgeInsetsMake(64, 0, 49, 0);
    [self updata:3];
    [self classbutton];
    
}

-(void)updata:(int)ID{
//    NSLog(@"sad");
//    self.arryM=[NSMutableArray array];
    AFHTTPSessionManager *mager=[AFHTTPSessionManager manager];
    
//    NSString *url=@"http://data.live.126.net/livechannel/classify/3/1.json";
    NSString *url=[NSString stringWithFormat:@"http://data.live.126.net/livechannel/classify/%d/1.json",ID];
    [mager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        for (NSDictionary *dict in responseObject[@"live_review"]) {
            
           if ([dict[@"liveStatus"] intValue]==0) {
                WFmodelHot *model=[WFmodelHot WFmodelWithdict:dict];
                [self.arryM addObject:model];

            }else{
                WFclassmodel *model=[WFclassmodel WFclassWithdict:dict];
                [self.arryM addObject:model];

            }
        }
        
        

        [self.tableView reloadData];
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"%@",error);
    }];
    

    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return self.arryM.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WFclassmodel *model=self.arryM[indexPath.section];
    if ([model.liveStatus intValue]==2) {
        static NSString *ID=@"lookCell";
        WFlookbackTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if (!cell) {
            cell=[WFlookbackTableViewCell cell];
        }
        cell.model=model;
        return cell;
        
    }else{
        static NSString *ID=@"Hotcell";
        WFmodelHot *model=self.arryM[indexPath.section];
        WFhotTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
        if (!cell) {
            cell=[WFhotTableViewCell cell];
        }
        cell.model=model;
        return  cell;
        
    }
    


    

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;

    }


#pragma mark 头部导航
-(void)classbutton{
    self.arr=@[@"TOP100",@"大直播",@"大现场",@"星在线",@"纵横谈",@"资讯",@"娱乐",@"本地",@"体育",@"时尚",@"汽车",@"科技",@"财经"];
    self.titleviews=[[UIView alloc]initWithFrame:CGRectMake(0, 0,self.view.frame.size.width , 180)];
    
    
    int column =5;
    CGFloat margin=15;
    
    CGFloat buttonW=(self.view.frame.size.width-2*margin-(column-1)*margin)/column;
    for (int i=0; i<self.arr.count; i++) {
        UIButton *titlebutton=[UIButton buttonWithType:UIButtonTypeCustom];
        //循环行数和列数
        CGFloat x=i%column;
        CGFloat y=i/column;
        titlebutton.frame=CGRectMake(margin+x*(buttonW+margin),30+ y*(30+margin), buttonW, 30);
        titlebutton.layer.cornerRadius=titlebutton.frame.size.height/2;
        
        self.titleviews.layer.masksToBounds = NO;
        
//        [titlebutton setBackgroundImage:[UIImage imageNamed:@"icon_news_channel_edit_border_norm2al_111x34_"] forState:UIControlStateNormal];
        //设置button的宽度和颜色
        titlebutton.layer.borderWidth=1.0;
        
        titlebutton.layer.borderColor=[UIColor grayColor].CGColor;
        //取消高亮
//        [titlebutton setAdjustsImageWhenHighlighted:NO];
        [titlebutton setTitle:self.arr[i] forState:UIControlStateNormal];
        [titlebutton setTitle:self.arr[i] forState:UIControlStateSelected];
        [titlebutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [titlebutton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        
        titlebutton.tag=i+3;
        [titlebutton addTarget:self action:@selector(Click:) forControlEvents:UIControlEventTouchUpInside];
        [self.titleviews addSubview:titlebutton];
    }
    self.tableView.tableHeaderView=_titleviews;
}
-(void)Click:(UIButton *)sender{
    self.arryM=nil;
    [self updata:(int)sender.tag];
    [self.mytableview reloadData];
    self.buttonRemb.selected=NO;
    
    sender.selected=YES;
    if (sender.selected) {
        [sender setBackgroundColor:[UIColor redColor]];
    }
    if (self.buttonRemb.selected==NO) {
        self.buttonRemb.backgroundColor=[UIColor clearColor];
    }
    self.buttonRemb=sender;
    
}

-(NSMutableArray *)arryM{
    if (!_arryM) {
        _arryM=[NSMutableArray array];
    }
    return _arryM;
}
-(NSMutableArray *)arryMback{
    if (!_arryMback) {
        _arryMback=[NSMutableArray array];
    }
    return _arryMback;
}
@end
